<template>
  <div class="data-wrapper">
    <div class="tree">
      <el-radio-group class="custom-radio-group" v-model="form.permission">
        <el-radio
          v-for="(item, index) of dict['role_data_manage']"
          :key="index"
          :value="item.value"
          >{{ item.name }}</el-radio
        >
      </el-radio-group>
    </div>
    <div class="bottom">
      <el-button @click="handleCancle" :loading="loading">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading"
        >保存</el-button
      >
    </div>
  </div>
</template>
<script setup>
import { ref, watch } from 'vue'
import { getDataPri, dataAddBatch } from '@/api/manager/systemManage/menu.js'
import { useDict } from '@/hooks/useDict'
const { dict } = useDict(['role_data_manage'])
const props = defineProps({
  roleId: {
    type: String,
  },
})
const form = ref({})
const initData = async () => {
  const res = await getDataPri({ roleId: props.roleId })
  form.value = res.data || {}
}
watch(
  () => props.roleId,
  (newVal) => {
    if (newVal) {
      initData()
    }
  },
  {
    immediate: true,
  },
)
const loading = ref(false)
const handleSubmit = async () => {
  const params = {
    pkid: form.value.pkid,
    roleId: props.roleId,
    permission: form.value.permission,
  }
  loading.value = true
  try {
    const res = await dataAddBatch(params)
    if (res.success) {
      ElMessage.success('保存成功')
    } else {
      ElMessage.error(res.message)
    }
    loading.value = false
  } catch (error) {
    loading.value = false
  }
}
const handleCancle = () => {
  initData()
}
</script>
<style lang="scss" scoped>
.data-wrapper {
  width: 100%;
  height: 100%;
  background-color: #fff;
  border-radius: 0 0 4px 4px;
  .tree {
    width: 100%;
    height: calc(100% - 70px);
    overflow-y: auto;
    box-sizing: border-box;
    padding: 20px 30px;
    .custom-radio-group {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
    }
  }
  .bottom {
    width: 100%;
    height: 70px;
    box-sizing: border-box;
    border-top: 1px solid #d5deed;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
